package com.bw.skill.mvvm.viewmodel;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.entity.ShoppDaoEntity;
import com.bw.greendao.entity.ShoppEntity;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.skill.R;
import com.bw.skill.action.SkillAction;
import com.bw.skill.adapter.SeckillAdapter;
import com.bw.skill.entity.AddSecKillEnttiy;
import com.bw.skill.entity.SecKillEntity;
import com.bw.skill.mvvm.model.SecKillModel;
import com.bw.skill.mvvm.view.SkillDetailActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author KK
 * @description:
 * @date :2021/10/26 16:12
 */
public class SecKillViewModel extends BaseViewModel<SecKillModel> implements OnItemChildClickListener {
    String nowString;
    public SeckillAdapter adapter = new SeckillAdapter();
    public ObservableField<String> starttime = new ObservableField<>();
    public ObservableField<String> endtime = new ObservableField<>();
    long times;
    @Override
    public void oncreate() {
        super.oncreate();

        nowString = TimeUtils.getNowString();
        LogUtils.e("timeeeeeeeeeeeeeee"+nowString);
        adapter.setOnItemChildClickListener(this);
        HashMap<String, Object> map = new HashMap<>();
        m.requestSeckill(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if(baseEntity.getMessage().equals("列表获取成功")){
            SecKillEntity secKillEntity = (SecKillEntity) baseEntity;
            adapter.setNewInstance(secKillEntity.getData());
            int starttime = secKillEntity.getData().get(0).getStarttime();
            long start = starttime;
            Date date = TimeUtils.millis2Date(start*1000);
            String s = TimeUtils.date2String(date);

            int endtime = secKillEntity.getData().get(0).getEndtime();
            long end = endtime;
            Date date1 = TimeUtils.millis2Date(end*1000);
            String s1 = TimeUtils.date2String(date1);
            this.starttime.set(s);
            this.endtime.set(s1);

            SkillAction action = new SkillAction();
            action.action = "SkillListActivity";
            action.nowTime = secKillEntity.getData().get(0).getNowtime();
            EventBus.getDefault().post(action);

        }
        if(baseEntity.getMessage().equals("添加购物车成功")){
            showMsg("添加在线购物车成功");
        }
        if(baseEntity.getMessage().equals("存货不足")){
            showMsg("哎呀呀,卖完啦,下次早点哟！");
        }
    }
    HashMap<String, Object> addShop = new HashMap<>();
    public void requestSkill(long nowTime){
        times = nowTime;
        addShop.put("time",nowTime);
        m.requestAddSecKill(addShop)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }

    @Override
    public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {

        SecKillEntity.DataBean item = (SecKillEntity.DataBean) adapter.getItem(position);
        List<UserEntity> list = DaoSessionUtils.getInstance().getSession().queryBuilder(UserEntity.class).list();
        if(list.get(0).getFlag()){
            //登录状态
            if(R.id.skill_buy == view.getId()){
                //加入购物车
                addShop = new HashMap<>();
                addShop.put("goodsCount",item.getGoods_num());
                addShop.put("goodsDesc",item.getGoods_desc());
                addShop.put("goodsIcon",item.getGoods_default_icon());
                addShop.put("goodsId",item.getId());
                addShop.put("goodsPrice",item.getGoods_default_price());
                addShop.put("goodsSku",item.getGoods_default_sku());
                addShop.put("userId",list.get(0).getUser_id());
                SkillAction action = new SkillAction();
                action.action = "SkillListGetNowTime";
                EventBus.getDefault().post(action);
            }else if(R.id.item_skill_title == view.getId()){
                HashMap<String, Object> map = new HashMap<>();
                Bundle bundle = new Bundle();
                bundle.putInt("goodsCount",item.getGoods_num());
                bundle.putString("goodsDesc",item.getGoods_desc());
                bundle.putString("goodsIcon",item.getGoods_default_icon());
                bundle.putInt("goodsId",item.getId());
                bundle.putString("goodsPrice",item.getGoods_default_price());
                bundle.putString("goodsSku",item.getGoods_default_sku());
                bundle.putInt("userId",list.get(0).getUser_id());
                bundle.putLong("time",item.getNowtime());
                map.put(UIField.PAGECLAZZ, SkillDetailActivity.class);
                map.put(UIField.PAGEBUNDLE,bundle);
                startPage(map);


            }
        }else {
            showMsg("请先登录");
        }
    }


    public void finishs(){
        finishPage();
    }
}
